package dbutil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;


public class ConnectDB {
	public static Connection connect;
	public static ResultSet resultSet;
	public static Statement statement; 
	//TODO: check this to use PreparedStatement

	/**************************************
	 * Method name		: connectDB
	 * Return type		: void
	 * Decription		: tao ket noi den CSDL, dung datapool
	 * Created date		: May 2, 2008
	 **************************************/

	public static void connectDB() {
		try{
			connect = Connect.SINGLETON.getConnection();
		} catch (Exception se) {
			System.out.println("Couldn't connect");
			se.printStackTrace();
		}
		try {
			statement = connect.createStatement();

		} catch (SQLException s) {
			System.out
					.println("We got an exception while creating a statement:"
							+ "that probably means we're no longer connected.");
			s.printStackTrace();
		}
		
	}
	
//	/**************************************
//	 * Method name		: connectDB
//	 * Return type		: void
//	 * Decription		: tao ket noi den CSDL, khởi tạo đối tượng statement
//	 * Created date		: May 2, 2008
//	 **************************************/
//	public static void connectDB() {
//		try {
//			Class.forName("org.postgresql.Driver");
//		} catch (ClassNotFoundException cnfe) {
//			System.out.println("Couldn't find the driver!");
//			cnfe.printStackTrace();
//		}
//		try {
//			String url = "jdbc:postgresql://localhost/quanlyhochieu";
//			connect = DriverManager.getConnection(url, "postgres", "postgres");
//		} catch (SQLException se) {
//			System.out.println("Couldn't connect");
//			se.printStackTrace();
//		}
//		try {
//			statement = connect.createStatement();
//			
//		} catch (SQLException s) {
//			System.out
//			.println("We got an exception while creating a statement:"
//					+ "that probably means we're no longer connected.");
//			s.printStackTrace();
//		}
//	}

	/**************************************
	 * Method name		: getDataTypeList
	 * Return type		: ArrayList<Map<String, Object>>
	 * Decription		:
	 * Created date		: Jun 2, 2008
	 **************************************/
	public static ArrayList<HashMap<String, Object>> getDataTypeList(String sql) {

		ConnectDB.getConnect();
		ArrayList<HashMap<String, Object>> listData = new ArrayList<HashMap<String, Object>>();
		try {
			resultSet = statement.executeQuery(sql);
			listData = DBUtils.putDataIntoList(resultSet);
			//connect.close();
		} catch (SQLException se) {
			se.printStackTrace();
		}
		
		return listData;
	}

	public static Connection getConnect() {
		return connect;
	}

	public static void setConnect(Connection connect) {
		ConnectDB.connect = connect;
	}

	public static Statement getStatement() {
		return statement;
	}

	public static void setStatement(Statement statement) {
		ConnectDB.statement = statement;
	}
}
